package com.cy.pj.sys.service.impl;


import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.Node;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 菜单业务对象
 */
@Transactional(readOnly = false,      //事务控制
        rollbackFor = Throwable.class,   //业务回滚
        isolation = Isolation.READ_COMMITTED) //读取多少并发不要出现脏读现象,隔离级别
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    @Transactional(readOnly = true) //查询建议readOnly = true
    //多事务并发执行 性能好
    @Override
    public List<SysMenu> findMenus() {
        //方法1
        return sysMenuDao.selectMenus();
//        //方法2(拓展)
//        //查找多有菜单信息:select * from sys_menus
//        List<SysMenu> menus =  sysMenuDao.selectMenus();
//        //在业务层对数据进行封装(一级菜单,二级菜单,三级菜单,...),递归算法
//        menus.stream().filter().collect();
//               //获取    过滤      收集
    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> findMenuTreeNodes() {

        return sysMenuDao.selectMenuTreeNodes();
    }

    @Override
    public int saveMenu(SysMenu menu) {
        return sysMenuDao.insertMenu(menu);
    }

    @Transactional(readOnly = true)
    @Override
    public SysMenu findById(Integer id) {
        return sysMenuDao.selectById(id);
    }

    @Override
    public int updateMenu(SysMenu menu) {
        int rows =  sysMenuDao.updateMenu(menu);
        if (rows==0){
            //   throw  new RuntimeException("记录已经不存在了");
            throw  new ServiceException("这个记录不存在");}
        else return rows;

//        //合并更新和新增
//        if (menu.getId()==null){
//            return  sysMenuDao.insertMenu(menu);
//        }

        //return sysMenuDao.updateMenu(menu);
    }


}
